package ciyuan.Block.TypeInterface;

import ciyuan.MainClass;
import com.mojang.blaze3d.matrix.MatrixStack;
import com.mojang.blaze3d.systems.RenderSystem;
import net.minecraft.client.gui.screen.inventory.ContainerScreen;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.text.ITextComponent;

public abstract class MachineScreen<T extends MachineContainer> extends ContainerScreen<T> {

    private static final ResourceLocation JIAN_TOU = new ResourceLocation(MainClass.MODID, "textures/gui/container/jiantou.png");
    private static final ResourceLocation FIRE = new ResourceLocation(MainClass.MODID, "textures/gui/container/fire.png");

    private final ResourceLocation backgroundResource;
    private final int textureWidth;
    private final int textureHeight;

    public MachineScreen(T cntainer, PlayerInventory playerInventory, ITextComponent titleText, ResourceLocation backgroundResource, int textureWidth, int textureHeight) {
        super(cntainer, playerInventory, titleText);
        this.backgroundResource = backgroundResource;
        this.textureWidth = textureWidth;
        this.textureHeight = textureHeight;
    }

    @Override
    protected void renderBg(MatrixStack matrixStack, float particleTicks, int mouseX, int mouseY) {
        renderBackground(matrixStack);
        int left = (this.width - textureWidth) / 2;
        int top = (this.height - textureHeight) / 2;

        RenderSystem.color4f(1.0F, 1.0F, 1.0F, 1.0F);
        this.minecraft.getTextureManager().bind(backgroundResource);
        blit(matrixStack, left, top, 0, 0, this.textureWidth, this.textureHeight, this.textureWidth, this.textureHeight);
    }

    @Override
    public void render(MatrixStack matrixStack, int mouseX, int mouseY, float particleTicks) {
        super.render(matrixStack, mouseX, mouseY, particleTicks);
        this.renderTooltip(matrixStack, mouseX, mouseY);
    }

    protected void drawJianTou(MatrixStack matrixStack, int leftPos, int topPos) {
        MachineTileEntity machineTileEntity = this.getMenu().getMachineTileEntity();
        int progress = (int) (machineTileEntity.getProgress() / (double) machineTileEntity.getConsumeTime() * 24);
        RenderSystem.color4f(1.0F, 1.0F, 1.0F, 1.0F);
        this.minecraft.getTextureManager().bind(JIAN_TOU);
        blit(matrixStack, this.leftPos + leftPos, this.topPos + topPos, 0, 0, progress, 17, 24, 17);
    }

    protected void drawFire(MatrixStack matrixStack, int leftPos, int topPos) {
        int status = this.getMenu().getMachineTileEntity().getMachineStatus() ? 14 : 0;
        RenderSystem.color4f(1.0F, 1.0F, 1.0F, 1.0F);
        this.minecraft.getTextureManager().bind(FIRE);
        blit(matrixStack, this.leftPos + leftPos, this.topPos + topPos, 0, 0, status, 14, 14, 14);
    }

}
